/**
 * 
 */
package com.kelaite.procurement.restful;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.kelaite.procurement.entity.TCity;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TProvince;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.service.CityService;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.service.ProvinceService;
import com.kelaite.procurement.service.UserService;

@Controller
@RequestMapping("/api")
public class CityRestController {
	
	private static final Logger logger = (Logger) LoggerFactory.getLogger(CityRestController.class);
	
	@Autowired
	private CityService cityService;
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private UserService userService;
	@Autowired
	private CompanyService companyService;
	
	@ResponseBody
	@RequestMapping(value = "/city", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<TCity> test() {
		//List<TCity> cityList = cityService.getByProperty("cityName", "shanghai");
		TCity city = this.cityService.getObject(2l);
		TProvince province = city.gettProvince();
		logger.debug("provinceName:" + province.getProvinceName());
		return new ResponseEntity<TCity>(city, HttpStatus.OK);
	}
	
	@ResponseBody
	@RequestMapping(value = "/{provinceId}/city", method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<TCity> deleteuser(@PathVariable("provinceId")long provinceId) {
		TProvince province = provinceService.getObject(provinceId);
		return cityService.findByTProvince(province);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/{companyId}/user", method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<TUser> changeUser(@PathVariable("companyId")long companyId) {
		List<TCompany> company = companyService.getByProperty("id", companyId);
		
		List<TUser> user = userService.getByProperty("tCompany", company.get(0));
		
		return user;
	}
	
}
